package com.chenxin.wms.controller;

import com.chenxin.common.annotation.Log;
import com.chenxin.common.constant.WmsConstants;
import com.chenxin.common.core.controller.BaseController;
import com.chenxin.common.core.domain.AjaxResult;
import com.chenxin.common.core.domain.ProcResult;
import com.chenxin.common.core.page.TableDataInfo;
import com.chenxin.common.enums.BusinessType;
import com.chenxin.common.enums.WmsTaskSubType;
import com.chenxin.common.utils.poi.ExcelUtil;
import com.chenxin.wms.domain.TaskEntity;
import com.chenxin.wms.domain.dto.OutboundConfirmParams;
import com.chenxin.wms.domain.vo.TaskVO;
import com.chenxin.wms.process.outbound.IProcOutboundConfirmWip;
import com.chenxin.wms.service.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 任务查询Controller
 *
 * @author gry
 * @date 2025-04-10
 */
@RestController
@RequestMapping("/wms/task")
public class TaskController extends BaseController {
    @Autowired
    private ITaskService taskService;
    @Autowired
    private IProcOutboundConfirmWip procOutboundConfirmWip;

    /**
     * 查询任务查询列表
     */
    @PreAuthorize("@ss.hasPermi('wms:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(TaskVO taskVo) {
        startPage();
        List<TaskVO> list = taskService.selectTaskList(taskVo);
        return getDataTable(list);
    }

    /**
     * 查询盘点任务查询列表
     */
    @GetMapping("/checkList")
    public TableDataInfo checkList(TaskVO taskVo) {
        startPage();
        List<TaskVO> list = taskService.selectCheckTaskList(taskVo);
        return getDataTable(list);
    }

    /**
     * 查询盘点出库任务查询列表
     */
    @GetMapping("/checkOutList")
    public TableDataInfo checkOutList(TaskVO taskVo) {
        startPage();
        taskVo.setTaskSubType(WmsTaskSubType.CHECK_OUT.getCode());
        List<TaskVO> list = taskService.selectTaskList(taskVo);
        return getDataTable(list);
    }

    /**
     * 原料半成品出库确认
     *
     * @param params
     * @return
     */
    @PostMapping("/confirmWip")
    public AjaxResult confirmWip(@RequestBody OutboundConfirmParams params) {
        ProcResult procResult = procOutboundConfirmWip.call(params);
        if (WmsConstants.PROC_SUCCESS.equals(procResult.getCode())) {
            return AjaxResult.success(procResult.getMessage());
        } else {
            return AjaxResult.error(procResult.getMessage());
        }
    }

    /**
     * 导出任务查询列表
     */
    @PreAuthorize("@ss.hasPermi('wms:task:export')")
    @Log(title = "任务查询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TaskVO taskVo) {
        List<TaskVO> list = taskService.selectTaskList(taskVo);
        ExcelUtil<TaskVO> util = new ExcelUtil<>(TaskVO.class);
        util.exportExcel(response, list, "任务查询数据");
    }

    /**
     * 获取任务查询详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:task:query')")
    @GetMapping(value = {"/", "/{taskId}"})
    public AjaxResult getInfo(@PathVariable(value = "taskId", required = false) Long taskId) {
        return success(taskService.selectTaskByTaskId(taskId));
    }

    /**
     * 新增任务查询
     */
    @PreAuthorize("@ss.hasPermi('wms:task:add')")
    @Log(title = "任务查询", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TaskEntity taskEntity) {
        return toAjax(taskService.insertTask(taskEntity));
    }

    /**
     * 修改任务查询
     */
    @PreAuthorize("@ss.hasPermi('wms:task:edit')")
    @Log(title = "任务查询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TaskEntity taskEntity) {
        return toAjax(taskService.updateTask(taskEntity));
    }

    /**
     * 取消任务
     */
    @PreAuthorize("@ss.hasPermi('wms:task:cancel')")
    @PostMapping("/cancel/{taskIds}")
    public AjaxResult cancel(@PathVariable Long[] taskIds) {
        ProcResult result = taskService.cancelTaskByIds(taskIds, getUserId());
        if (WmsConstants.PROC_SUCCESS.equals(result.getCode())) {
            return success(result.getMessage());
        } else {
            return error(result.getMessage());
        }
    }

    /**
     * 删除任务查询
     */
    @PreAuthorize("@ss.hasPermi('wms:task:remove')")
    @Log(title = "任务查询", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds) {
        return toAjax(taskService.deleteTaskByTaskIds(taskIds));
    }
}
